package com.xudm.cdg.config;

import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


/**
 * redis cache configuration。
 */
@Configuration
public class RedisConfig implements CachingConfigurer {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        FastJson2JsonRedisSerializer<String> serializer = new FastJson2JsonRedisSerializer<>(String.class);
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setHashValueSerializer(serializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

//    /**
//     * 配置分布式锁
//     * @return redisson
//     */
//    @Bean
//    public RedissonClient redissonClient(RedisProperties redisProperties) {
//        Config config = new Config();
//        List<String> nodes = redisProperties.getCluster().getNodes();
//        List<String> nodeAddresses= new ArrayList<>();
//        for (String node : nodes) {
//            nodeAddresses.add("redis://"+node);
//        }
//        config.useClusterServers().setPassword(redisProperties.getPassword()).setNodeAddresses(nodeAddresses);
//        config.setCodec(new JsonJacksonCodec());
//        return Redisson.create(config);
//    }
}


